/**
    ******************************************************************************
    * @file    queue.c
    * @author  debian
    * @version V1.0.0
    * @date    2020-07-29
    * @brief   队列
    ******************************************************************************
    * @attention
    *
    *
    ******************************************************************************
    */ 

/* Includes ------------------------------------------------------------------*/
#include "queue.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

/** @addtogroup DataStruct_Driver
    * @{
    */

/** @addtogroup queue
    * @{
    */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/


/** @defgroup queue_Exported_Functions queue Exported Functions
    * @{
    */

/** @defgroup queue_Exported_Functions_Group1 Initialization and deinitialization functions
    *  @brief    Initialization and Configuration functions
    *
@verbatim    
    ===============================================================================
                ##### Initialization and deinitialization functions #####
    ===============================================================================
    [..]
        This section provides functions allowing to initialize and de-initialize the queue
        to be ready for use.
 
@endverbatim
    * @{
    */ 

#define DEFAULT_SIZE    50

/**
    * @brief  创建queue对象
    * @param  
    * @retval 
    */ 
    queue_t* queue_creat(int size)
    {
        queue_t *queue = malloc(sizeof(queue_t));
        if(queue == NULL)  return NULL;

        if(size > 0) {
            queue->capcity = size;
        } else {
            queue->capcity = DEFAULT_SIZE;
        }

        queue->task = malloc(sizeof(task_t) * queue->capcity);
        if(queue->task == NULL) {
            free(queue);
            return NULL;
        }

        queue->head = 0;
        queue->size = 0;
        queue->tail = 0;

        return queue;
    }

/**
    * @brief  销毁queue对象
    * @param  
    * @retval 
    */ 
    int queue_destroy(queue_t* queue)
    {
        free(queue->task);
        free(queue);

        return 0;
    }
    

    int queue_is_full(queue_t* queue)
    {
        return queue->capcity == queue->size;
    } 

    int queue_is_empty(queue_t* queue)
    {
        return queue->size == 0;
    }


    int queue_push_tail(queue_t* queue, task_t *task)
    {
        if(!queue_is_full(queue)) {
            queue->task[queue->tail].run = task->run;
            queue->task[queue->tail].arg = task->arg;

            queue->tail = (queue->tail + 1) % queue->capcity;
            queue->size++;
            return 0;
        }
        
        return -1;
    }

    task_t* queue_pop_head(queue_t* queue)
    {
        if(!queue_is_empty(queue)) {
            task_t* task = malloc(sizeof(task_t));
            task->arg = queue->task[queue->head].arg;
            task->run = queue->task[queue->head].run;

            queue->head = (queue->head + 1) % queue->capcity;
            queue->size--;
            //记得释放内存
            return task;
        }
        
        return NULL;
    }

    int queue_pop_free(task_t* t)
    {
        free(t);
        return 0;
    }
/**
    * @}
    */

/** @defgroup queue_Exported_Functions_Group2 operation functions 
    *  @brief   operation functions
    *
@verbatim   
    ===============================================================================
                        ##### operation functions #####
    ===============================================================================
    [..]
        This subsection provides a set of functions allowing to manage the queue.

@endverbatim
    * @{
    */

        /* 操作函数写在这里 */

    /**
    * @}
    */


/**
    * @}
    */

/**
    * @}
    */

/**
    * @}
    */

/************************ (C) 2020 HOME 深圳龙华 *****END OF FILE****/

